<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('新增上传课件')" />
    <th:block th:include="include :: datetimepicker-css" />
    <th:block th:include="include :: bootstrap-fileinput-css"/>
    <th:block th:include="include :: ztree-css" />
</head>
<body class="white-bg">
    <div class="wrapper wrapper-content animated fadeInRight ibox-content">
        <form class="form-horizontal m" id="form-file-add">
            <div class="form-group">    
                <label class="col-sm-3 control-label">课件名称：</label>
                <div class="col-sm-8">
                    <input id="coursewareName" name="coursewareName" class="form-control" type="text">
                </div>
            </div>
<!--            <div class="form-group">-->
<!--                <label class="col-sm-3 control-label">学科类型：</label>-->
<!--                <div class="col-sm-8">-->
<!--                    <textarea name="coursewarePrimary" class="form-control"></textarea>-->
<!--                </div>-->
<!--            </div>-->
            <div class="form-group">
                <label class="col-sm-3 control-label">学科类型：</label>
                <div class="col-sm-8">
                    <label class="check-box">
                        <input type="checkbox" value="1">展开/折叠</label>
                    <label class="check-box">
                        <input type="checkbox" value="2">全选/全不选</label>
                    <label class="check-box">
                        <input type="checkbox" value="3" checked>父子联动</label>
                    <div id="menuTrees" class="ztree ztree-border"></div>
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">课件文件：</label>
                <div class="col-sm-8">
                    <input type="hidden" name="coursewareFile">
                    <div class="file-loading">
                        <input class="form-control file-upload" id="coursewareFile" name="file" type="file">
                    </div>
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">课件图标：</label>
                <div class="col-sm-8">
                    <input type="hidden" name="coursewareIcon">
                    <div class="file-loading">
                        <input class="form-control file-upload" id="coursewareIcon" name="file" type="file">
                    </div>
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">上传时间：</label>
                <div class="col-sm-8">
                    <div class="input-group date">
                        <input id="fileTime" name="fileTime" class="form-control" placeholder="yyyy-MM-dd" type="text">
                        <span class="input-group-addon"><i class="fa fa-calendar"></i></span>
                    </div>
                </div>
            </div>
        </form>
    </div>
    <th:block th:include="include :: footer" />
    <th:block th:include="include :: datetimepicker-js" />
    <th:block th:include="include :: bootstrap-fileinput-js"/>
    <th:block th:include="include :: ztree-js" />
    <script th:inline="javascript">

        $(function() {
            var url = ctx + "system/subject/treeData";
            var options = {
                id: "menuTrees",
                url: url,
                check: { enable: true },
                expandLevel: 0
            };
            $.tree.init(options);
        });


        $('input').on('ifChanged', function(obj){
            var type = $(this).val();
            var checked = obj.currentTarget.checked;
            if (type == 1) {
                if (checked) {
                    $._tree.expandAll(true);
                } else {
                    $._tree.expandAll(false);
                }
            } else if (type == "2") {
                if (checked) {
                    $._tree.checkAllNodes(true);
                } else {
                    $._tree.checkAllNodes(false);
                }
            } else if (type == "3") {
                if (checked) {
                    $._tree.setting.check.chkboxType = { "Y": "ps", "N": "ps" };
                } else {
                    $._tree.setting.check.chkboxType = { "Y": "", "N": "" };
                }
            }
        })

        var prefix = ctx + "system/file"
        $("#form-file-add").validate({
            focusCleanup: true
        });

        function submitHandler() {
            if ($.validate.form()) {
                add();
            }
        }

        function add() {
            var coursewareName = $("input[name='coursewareName']").val();
            var coursewareFile = $("input[name='coursewareFile']").val();
            var coursewareIcon = $("input[name='coursewareIcon']").val();
            var fileTime = $("input[name='fileTime']").val();
            var menuIds = $.tree.getCheckedNodes();
            $.ajax({
                cache : true,
                type : "POST",
                url : ctx + "system/file/add",
                data : {
                    "coursewareName": coursewareName,
                    "coursewareFile": coursewareFile,
                    "coursewareIcon": coursewareIcon,
                    "fileTime": fileTime,
                    "menuIds": menuIds
                },
                async : false,
                error : function(request) {
                    $.modal.alertError("系统错误");
                },
                success : function(data) {
                    alert(coursewareName)
                    $.operate.successCallback(data);
                }
            });
        }

        $("input[name='fileTime']").datetimepicker({
            format: "yyyy-mm-dd",
            minView: "month",
            autoclose: true
        });

        $(".file-upload").fileinput({
            uploadUrl: ctx + 'common/upload',
            maxFileCount: 1,
            autoReplace: true
        }).on('fileuploaded', function (event, data, previewId, index) {
            $("input[name='" + event.currentTarget.id + "']").val(data.response.url)
        }).on('fileremoved', function (event, id, index) {
            $("input[name='" + event.currentTarget.id + "']").val('')
        })
    </script>
</body>
</html>